#!/bin/bash

set -e

if [ $# -lt 2 ]; then
    echo "Usage: $0 <platform> <node_index> <total_nodes>"
    exit 1
fi

PLATFORM=$1
NODE_INDEX=$2
TOTAL_NODES=$3

TEST_FILES=$(find e2e/flows -name "*.yml" | sort | awk "NR % $TOTAL_NODES == $NODE_INDEX")

EXIT_CODE=0

for TEST_FILE in $TEST_FILES; do
  echo "Running test: $TEST_FILE"
  if ! maestro test "$TEST_FILE" "@.env.maestro.$PLATFORM"; then
    EXIT_CODE=1
  fi
done

if [ $EXIT_CODE -ne 0 ]; then
  echo "At least one test failed — uploading screenshots and notifying Slack"
  ./scripts/ci/report-maestro-test-failure "$PLATFORM"
fi

exit $EXIT_CODE
